package com.pro.utils;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * auther 杨沛琪
 * createDate 2021/11/4
 */
public class InformationUtils {
    //获取mysql版本号
    public static String getMysqlVerseion() {
        Connection conn = null;
        String version = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql:///siyun", "root", "root");

            DatabaseMetaData metaData = (DatabaseMetaData) conn.getMetaData();

            version = metaData.getDatabaseProductVersion();//得到数据库版本信息

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (null != conn) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return version;
    }

    //获取服务器名称、IP地址、java版本号、端口号、操作系统
    public static String getServerName() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        String hostName = localHost.getHostName();
        return hostName;
    }

    //获取服务器地址
    public static String getserverAddress() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();

        String hostAddress = localHost.getHostAddress();
        return hostAddress;
    }

     //获取端口号
     public static int getPortNumber(HttpServletRequest request) {
         int localPort = request.getLocalPort();
         return localPort;
     }

    //获取操作系统
    public static String getOperatingSystem() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        String os = System.getProperty("os.name");
        return os;
    }

    //获取浏览器名称
    public static String getIeName(HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IOException {
        // 获取浏览器信息
        Browser browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
        String ieName = browser.getName();
        return ieName;
    }
    //获取浏览器版本
    public static String getIeVersion(HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IOException {
        // 获取浏览器信息
        Browser browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
        // 获取浏览器版本号
        Version version = browser.getVersion(request.getHeader("User-Agent"));
        String ieVersion = version.getVersion();
        return ieVersion;
    }
}